package com.meihuak.cloud_ordering.controller;


import com.meihuak.cloud_ordering.bean.PostTable;
import com.meihuak.cloud_ordering.bean.Comment;
import com.meihuak.cloud_ordering.service.CommentService;
import com.meihuak.cloud_ordering.service.PostTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Slf4j
@Controller
@RequestMapping("/post")
public class PostTableController {

    @Autowired
    PostTableService postTableService;

    @Autowired
    CommentService commentService;


    /**
     * 添加帖子
     * @return 评论信息
     */
    @ResponseBody
    @RequestMapping("/addpost")
    public Map<String,Object> addComment(@RequestParam (value = "title")String title,
                                         @RequestParam(value = "content")String content,
                                         @RequestParam(value = "userid")int userid,
                                        @RequestParam(value = "ptype")String ptype){

        postTableService.createPost(title,content,userid,ptype);
        Map<String,Object> ret = new HashMap<>();
        ret.put("msg","success");
        return ret;
    }


    @ResponseBody
    @RequestMapping("/delpost")
    public Map<String,Object> delComment(@RequestParam(value = "id")int id) {
        postTableService.delPostTableById(id);
        Map<String,Object> ret = new HashMap<>();
        ret.put("msg","success");
        return ret;
    }

    @ResponseBody
    @RequestMapping("/showall")
    public Map<String,Object> getAllPost(@RequestParam(value = "i")int i,
                                         @RequestParam(value = "m")int m,
                                         @RequestParam(value = "ptype")String ptype){

        List<PostTable> posts = postTableService.getAllPost(i,m,ptype);
        Map<String,Object> ret = new HashMap<>();
        ret.put("posts",posts);
        ret.put("msg","success");
        return ret;
    }



}
